import { ref, watch, toRefs } from "vue"
//导入Http
import { Http } from "../../http/map"
//导入地图
import { useMapStore } from "@/stores/mapStore"
// 解构map,docLayer
const { mapState } = toRefs(useMapStore())
const content = ref("")
export const useGetLoaction = () => {
  const list = ref(null)
  watch(content, async (newQuestion, oldQuestion) => {
    let data = await Http.getTargetPlace(content.value)
    console.log(data)
    if (data) {
      if (data.pois) {
        list.value = data.pois
      } else if (data.area) {
        list.value = []
        list.value.push(data.area)
      } else {
        list.value = []
        console.log(list.value)
      }
    } else {
      list.value = null
    }
    if (content.value == "") {
      list.value = null
    }
  })
  //点击跳转
  function toTargetPlace(lonlat) {
    console.log(lonlat)
    lonlat = lonlat.split(" ")
    //设置飞行视角
    let view = mapState.value.map.getView()
    view.animate({
      center: lonlat,
      zoom: 12,
      duration: 2000,
    })
    list.value = null
    content.value = ""
  }
  //回到当前位置
  function toLocated() {
    let view = mapState.value.map.getView()
    view.animate({
      center: [114.379, 30.496],
      zoom: 12,
      duration: 2000,
    })
  }
  return { content, list, toTargetPlace, toLocated }
}
